package net.nshc.kwkim.mycalc.util;

import net.nshc.kwkim.mycalc.*;
import net.nshc.kwkim.mycalc.buttonlistener.*;
import android.util.*;

public class DisplayContentAnalyzer {
    
    public static boolean isTheLastCharacterNumber(String str) {
        if ((str == null) || (str.length() < 1))
            return false;
        
        char c = str.charAt(str.length() - 1);

        return Character.isDigit(c);
    }
    
    public static boolean isTheLastCharacterArithmeticOperator(String str) {
    	str = EqualSignButtonClickListener.changeExtendexp(str);
        if ((str == null) || (str.length() < 1))
            return false;
        
        char c = str.charAt(str.length() - 1);

        return isCharacterArithmeticOperator(c);
    }
    
    private static boolean isCharacterArithmeticOperator(char c) {
        switch (c) {
            case '+': return true;
            case '-': return true;
            case '×': return true;
            case '÷': return true;
            case 't': return true;
            case 'c': return true;
            case 's': return true;
            case 'l': return true;
            case 'f': return true;
            case 'n': return true;
            case 'x': return true;
            case 'b': return true;
            case 'q': return true;
            case 'p': return true;
        }
    
        return false;
    }
    
    public static boolean isTheLastCharacterPoint(String str) {
        if ((str == null) || (str.length() < 1))
            return false;
        
        char c = str.charAt(str.length() - 1);

        return isCharacterPoint(c);
    }
    
    private static boolean isCharacterPoint(char c) {
        return (c == '.') ? true : false;
    }

    public static String getTheLastOperandFrom(String expression) {
    	expression = EqualSignButtonClickListener.changeExtendexp(expression);
        if (expression == null)
            return null;
        if (expression.length() < 1)
            return null;
         
        int i = expression.length() - 1;
        
        for (; i >= 0; i--) {
            char c = expression.charAt(i);
            
            if (Character.isDigit(c))
                continue;
            else if (isCharacterPoint(c))
                continue;
            else if (isCharacterArithmeticOperator(c))
                break;
        }
        
        String theLastOperand = expression.substring(i + 1, expression.length());
        Log.d(LogDefine.LOG_CALCULATOIN, "DisPlayContentAnalyzer_i+1: " + (i+1) + ", expression.length(): " + expression.length());
        
        return theLastOperand;
    }

    public static boolean isOperandRealNumber(String operand) {
        return (countPoints(operand) == 1) ? true : false;
    }

    public static int countPoints(String operand) {
        int numberOfPoints = 0;
        final int lengthOfOperand = operand.length();
        
        for (int i = 0; i < lengthOfOperand; i++) {
            if (isCharacterPoint(operand.charAt(i)))
                numberOfPoints++;
        }
        
        return numberOfPoints;
    }
    
    public static boolean isEmpty(String content){
    	return ( content.equals("") || content.length() == 0) ? true : false;
    }
    
    public static boolean isOperator(String content){
    	for(int i = 0; i < content.length(); i++){
    		if(isCharacterArithmeticOperator(content.charAt(i)))
    			return true;
    	}
    	return false;
    }
}

